package com.lzx.orderserver.service.impl;

import com.lzx.orderserver.model.Borrow;
import com.lzx.orderserver.service.BorrowService;
import com.lzx.orderserver.utils.GenericsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

/**
 * @author 刘正星
 * @version v1.0
 * @package com.lzx.orderserver.service.impl
 * @data 2022/9/13 22:55
 */
@Service
public class BorrowServiceImpl<T extends Borrow> implements BorrowService<T> {

    /**
     * 获取 T.class
     */
    public Class<T> getTClass() {
        return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    }


    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public T save(T borrow) {
        return mongoTemplate.save(borrow);
    }

    @Override
    public List<T> getList() {
        return mongoTemplate.findAll(getTClass());
    }

    @Override
    public List<T> query(String type) {
        Class<T> tClass = this.getTClass();
        List<T> tList = mongoTemplate.find(new Query(Criteria.where("type").is(type)), tClass);
        return tList ;
    }


}
